<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CarPackageCategory;
use App\Admin\Services\CarService;
use App\Models\Brand;
use App\Models\Car;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CarPackageCategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(CarPackageCategory::with(['brand']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('brand.title', admin_trans('car-package-category.fields.brand_id'));
            $grid->column('title');
            $grid->column('car_ids')->display(function ($car_ids) {
                return CarService::carJsonSwitch($car_ids);
            });
            $grid->column('status')->using(admin_trans('car-package-category.options.status'));
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('title');
                $brand = Brand::all()->pluck('title', 'id');
                $filter->equal('brand_id')->select($brand);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, CarPackageCategory::with(['brand']), function (Show $show) {
            $show->field('id');
            $show->field('brand.title', admin_trans('car-package-category.fields.brand_id'));
            $show->field('title');
            $show->field('car_ids')->as(function ($car_ids) {
                return CarService::carJsonSwitch($car_ids);
            });
            $show->field('status')->using(admin_trans('car-package-category.options.status'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $car_options = Car::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id');

        return Form::make(new CarPackageCategory(), function (Form $form) use ($car_options) {
            $form->display('id');
            $form->select('brand_id')
                ->options(Brand::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->load('car_ids', 'package-category-has-car')
                ->required();
            $form->text('title')->required();
            $form->multipleSelect('car_ids')->options($car_options)->saveAsJson();
            $form->radio('status')->options(admin_trans('car-package-category.options.status'))->default('1');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function categoryHasCar(Request $request)
    {
        $brand_id = $request->get('q');

        return Car::where('brand_id', $brand_id)->get(['id', DB::raw('title as text')]);
    }
}
